home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Gfx / conv / GIFsplit.lha / source / GifSplit.rexx < prev   
OS/2 REXX Batch file  |  1998-02-17  |  6KB  |  250 lines

  1. /*
  2. ** GifSplit.rexx
  3. ** $ver: 0.99, 16 Feb 1998
  4. */
  5.  
  6. parse arg filename debug .
  7.  
  8. if debug~="" then debug=1
  9. else debug=0
  10.  
  11. if ~open('in',filename,'r') then 
  12. do
  13.    say "GifSplit.rexx"
  14.    Say "   Usage: Rx GifSplit <filename>"
  15.    say
  16.    Say "   This script will take an animated gif file"
  17.    Say "and split it into the individual gifs."
  18.    exit 10
  19. end
  20.  
  21. /* read identifier
  22. ** 6 bytes
  23. */
  24.  
  25. line = readch('in',6)
  26. if line ~="GIF89a"  & line ~="GIF87a" then
  27. do
  28.    say filename "is not an gif."
  29.    exit 10
  30. end
  31.  
  32. header=line
  33.  
  34. /* 
  35. ** LSD - Logical Screen Descriptor (no, not a drug)
  36. ** reads the logical screen descriptor, 7 bytes
  37. */
  38.  
  39. LSD=readch('in',7)
  40. width = c2d(reverse(substr(lsd,1,2)))
  41. height = c2d(reverse(substr(lsd,3,2)))
  42.  
  43. stuff = c2b(substr(lsd,5,1))
  44. GlobalColor = c2d(b2c(substr(stuff,1,1)))
  45. colorRes = c2d(b2c(substr(stuff,2,3))) +1
  46. TotalPalette = (2**colorres)**3
  47. sortflag = c2d(b2c(substr(stuff,5,1)))
  48. sizeotable = c2d(b2c(substr(stuff,6,3)))+1
  49. backgrd = c2d(substr(lsd,6,1))
  50. ratio = c2d(substr(lsd,7,1))
  51. if ratio=0 then ratio=1
  52.  
  53. header=header || lsd
  54. if debug then
  55. do
  56.    say "Screen Width     : "width
  57.    say "Screen Height    : "height
  58.    if GlobalColor then say "Global Color Map: Yes"
  59.    else say "Global Color Map: No"
  60.    say "Color Resolution : "ColorRes" bits ("TotalPalette" Total Palette)"
  61.    if Sortflag then say "Palette sorted by decreasing importance"
  62.    else say "Palette unsorted"
  63.    Say "Bitplanes        : "sizeotable" ("2**sizeotable" colors)"
  64.    say "Background color : #"backgrd
  65.    say "Pixel Ratio      : "ratio
  66. end
  67.  
  68.  
  69. /*
  70. ** read global color table (maybe)
  71. ** 3 bytes * 2^bitplates 
  72. */
  73.  
  74. gct=""
  75.  
  76. if Globalcolor=1 then 
  77. do
  78.    if debug then say "Reading global color table"
  79.  
  80.    GCT = readch('in',3*(2**sizeotable))
  81.  
  82.    if debug then
  83.    do x = 1 to (2**sizeotable) by 2
  84.       say "Color "||right(X,2,'0')||" RGB "c2x(substr(gct,(x-1)*3+1,3))"  Color "||right(x+1,2,'0')|| " RGB "c2x(substr(gct,(x-1)*3+2,3))
  85.    end
  86. end
  87.  
  88. /*
  89. ** Getting ready to split files.
  90. ** First, figure out name to use for files.  Double check
  91. ** to see if there's a ".GIF" extension.
  92. **
  93. ** Since I use this mostly for web gifs, many web banner type
  94. ** gifs don't have a ".GIF" Extension, so I want to be safe.
  95. */
  96.  
  97. filecount=1
  98. dt = lastpos('.',filename)
  99. sl = lastpos('/',filename)
  100. if dt=0 | dt<sl then
  101. do
  102.    filestart=filename
  103.    fileend=".Gif"
  104. end
  105. else
  106. do
  107.    filestart = left(filename,dt-1)
  108.    fileend = delstr(filename,1,dt-1)
  109. end
  110.  
  111. do while ~eof('in')
  112.  
  113.    nx = readch('in',1)
  114.    do while nx~="2C"x & Nx~='3B'x
  115.  
  116.       if nx = '21'x then
  117.       do
  118.          if debug then say 'Gif Extension code'
  119.          
  120.          bltype = readch('in',1)
  121.          len = c2d(readch('in',1))
  122.          
  123.          if debug then
  124.          do
  125.             select
  126.                when bltype = 'F9'x then say "Graphic Control Extension"
  127.                When bltype = 'FE'x then say "Comment Extension"
  128.                When bltype = '01'x then say "Play Text Extension"
  129.                When bltype = 'FF'x then say "Application Extension"
  130.                otherwise say "Unknown Extension of hex type "c2x(bltype)
  131.             end
  132.          end
  133.          
  134.          if bltype = 'FF'x then 
  135.          do
  136.             call readch('in',len) /* Application Extension type */
  137.             BlkLen = c2d(readch('in',1)) /* read length of extension type */
  138.             call readch('in',blklen) /* read Application Extension data */
  139.          end
  140.          else
  141.          do
  142.             call readch('in',len) /* read in other extension data */
  143.          end
  144.          
  145.          ending = readch('in',1)
  146.          if ending~='00'x then
  147.          do
  148.             say "No data terminator for this block, exiting..."
  149.             exit 10
  150.          end
  151.       end
  152.       else 
  153.       do
  154.          say "Somethings wrong.  Hex character "c2x(nx)" found.  Exiting..."
  155.          exit 10
  156.       end
  157.  
  158.       nx = readch('in',1)
  159.    end
  160.  
  161.    if nx='3b'x then 
  162.    do
  163.       say "File termination found, exiting..."
  164.       call close('in')
  165.       exit 0
  166.    end
  167.  
  168.    say 'Found image number 'filecount
  169.  
  170.    lct=""
  171.    code.=""
  172.    codecount = 0
  173.    codehead = nx||readch('in',9)
  174.  
  175.    imageleft = c2d(reverse(substr(codehead,2,2)))
  176.    imagetop = c2d(reverse(substr(codehead,4,2)))
  177.    imagewidth = c2d(reverse(substr(codehead,6,2)))
  178.    imageheight = c2d(reverse(substr(codehead,8,2)))
  179.    stuff = c2b(right(codehead,1))
  180.    localcolor = left(stuff,1)
  181.    interlace = substr(stuff,2,1)
  182.  
  183.    localsortflag = substr(stuff,3,1)
  184.    localcolortable = c2d(b2c(right(stuff,3)))+1
  185.  
  186.    if debug then
  187.    do
  188.       say "Image Left  : " imageleft
  189.       say "Image Top   : " Imagetop
  190.       say "Image Width : " Imagewidth
  191.       say "Image Height: " Imageheight
  192.       
  193.       if localcolor then say "Local Color Table : Yes"
  194.       else say "Local Color Table : No"
  195.       
  196.       if interlace then say "Interlaced"
  197.       else say "Not Interlaced"      
  198.       
  199.       if localSortflag then say "Palette sorted by decreasing importance"
  200.       else say "Palette unsorted"
  201.    end
  202.  
  203.    if localcolor=1 then 
  204.    do
  205.       if debug then say "Reading local color table"
  206.       
  207.       lCT = readch('in',3*(2**localcolortable))
  208.       
  209.       if debug then
  210.       do x = 1 to (2**localcolortable) by 2
  211.          say "Color "||right(X,2,'0')||" RGB "c2x(substr(Lct,(x-1)*3+1,3))"  Color "||right(x+1,2,'0')|| " RGB "c2x(substr(Lct,(x-1)*3+2,3))
  212.       end
  213.  
  214.    end
  215.  
  216.    bitsize = readch('in',1)
  217.    codecount = codecount+1
  218.    code.codecount = bitsize
  219.    codesize =readch('in',1)
  220.    code.codecount=code.codecount||codesize
  221.    chsz = c2d(codesize)
  222.    
  223.  
  224.    do while chsz>0
  225.       code.codecount = code.codecount|| readch('in',chsz)
  226.       codecount=codecount+1
  227.       codesize = readch('in',1)
  228.       chsz=c2d(codesize)
  229.       code.codecount = codesize
  230.    end
  231.  
  232.    if debug then say "End of image."
  233.  
  234.    /* write file */
  235.    
  236.    call open('out',filestart||'.'right(filecount,3,'0')||fileend,'W')
  237.       call writech('out',header||GCT|| codehead ||lct)
  238.       do x=1 to codecount
  239.          call writech('out',code.x)
  240.       end
  241.    call close('out')
  242.  
  243.    filecount=filecount + 1
  244.  
  245. end
  246.  
  247. say "?? Premature End of File???"
  248.  
  249. close('in')
  250.